# encoding = utf-8
import xgboost as xgb
from sklearn.model_selection import GridSearchCV
from application.logging import logger


def get_best_params_(train_x, train_y, cv_params, other_params):
    """
    获取最佳参数
    :param train_x:
    :param train_y:
    :param cv_params:
    :param other_params:
    :return:
    """
    #
    model = xgb.XGBRegressor(**other_params)
    #
    optimized_GBM = GridSearchCV(
        estimator=model,
        param_grid=cv_params,
        scoring="r2",
        cv=4,
        verbose=1,
        # n_jobs=4,
        n_jobs=8  # 并行进程数
    )
    optimized_GBM.fit(train_x, train_y)
    #
    evaluate_result = optimized_GBM.cv_results_
    # 打印结果
    # logger.info(f"每轮迭代运行结果:\n {evaluate_result}")
    logger.info(f"参数最佳取值: {optimized_GBM.best_params_}")
    logger.info(f"模型最佳得分: {optimized_GBM.best_score_}")
    # 返回最佳参数
    return optimized_GBM.best_params_
    pass


pass
if __name__ == '__main__':
    pass
